package com.chillis.standings.ui.table;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import com.chillis.standings.R;
import com.chillis.standings.domain.exceptions.CellTypeNotFoundException;
import com.chillis.standings.domain.table.CellType;
import com.chillis.standings.domain.table.Table;

import static com.chillis.standings.domain.table.CellType.*;

public class TableAdapter extends BaseAdapter {
   public TableAdapter(Context context, Table table) {
      if (table.stages().size() == 0) throw new Error("stages count can't be 0");

      //TODO fix it
      // 4 - number of rows needed to build representation of one match
      _size = table.stages().get(0).matches().size() * 4 * table.stages().size();
      _context = context;
   }

   @Override
   public int getCount() {
      return _size;
   }

   @Override
   public Object getItem(int position) {
      return null;
   }


   @Override
   public long getItemId(int position) {
      return 0;
   }

   @Override
   public int getItemViewType(int position) {
      return super.getItemViewType(position);
   }

   @Override
   public int getViewTypeCount() {
      return CellType.values().length;
   }

   private CellType getCellType(int position) {
      return position % 2 == 0 ? Empty : Player;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {

      View view;
      if (convertView == null) {
         view = LayoutInflater.from(_context).inflate(R.layout.cell, parent, false);
      } else {
         view = convertView;
      }

      CellType cellType = getCellType(position);
      if (cellType == Empty) {
         view.setBackgroundColor(Color.parseColor("#000000"));
      } else if (cellType == Player) {
         view.setBackgroundColor(Color.parseColor("#bbbbbb"));
      } else {
         throw new CellTypeNotFoundException();
      }

      return view;
   }

   private int _size = 0;
   private Context _context;
}
